<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Common\Model\NewsColumnEntranceModel;
use Common\Model\NewsModel;

class NewsColumnEntranceController extends AdminbaseController
{
	protected $news_column_entrance_model;
	protected $news_column_entrance_type_model;
	
	function _initialize()
	{
		parent::_initialize();
		$this->news_column_entrance_model = D("Common/NewsColumnEntrance");
		$this->news_column_entrance_type_model = D("Common/NewsColumnEntranceType");
	}

    /**
     * 入口列表
     * */
    function index()
    {
    	$column_id = I("column_id") ? I("column_id") : '';
    	$area_id = I("area_id") ? I("area_id") : '';
    	$id_type = '';
    	$id_val  = '';
    	if($column_id){
    		$id_type = "column_id";
    		$id_val  = $column_id;
    	}else{
    		$id_type = "area_id";
    		$id_val  = $area_id;
    	}
    	if(!$id_val){
    		$this->error("未选中栏目！");
    	}
    	$currentPage = I('param.p', 0);
    	if ($currentPage <= 0) {
    		$currentPage = 1;
    	}
    	
    	$pageSize = '20';
    	$pageStart = ($currentPage - 1) * $pageSize;
    	$pageEnd = $currentPage * $pageSize;
    	
    	$where = array();
    	if($id_val) $where[$id_type] = $id_val;
    	$column_entrance_data = $this->news_column_entrance_model->GetColumnEntranceList($where,$pageStart,$pageSize);
    	$column_entrance_list = $column_entrance_data['column_entrance_list'];
    	$count = $column_entrance_data['count'];

    	$page = $this->page($count ,10);
    	$this->assign("page", $page->show('Admin'));
    	$this->assign("list", $column_entrance_list);
    	$this->assign("id_type", $id_type);
    	$this->assign("id_val", $id_val);
    	$this->display();
    }
    
    
    /**
     * 添加栏目弹窗
     */
    public function addEntrance(){
    	$column_id = I("column_id") ? I("column_id") : '';
    	$area_id = I("area_id") ? I("area_id") : '';
    	$id_type = '';
    	$id_val  = '';
    	if($column_id){
    		$id_type = "column_id";
    		$id_val  = $column_id;
    	}else{
    		$id_type = "area_id";
    		$id_val  = $area_id;
    	}
    	$entrance_id = I("entrance_id");
    	 
    	if(!$id_val){
    		$this->error("参数错误！");
    	}
    	$info_data = '';
    	if($entrance_id){
    		$Obj = M("vrnews1.column_entrance", 'news_');
    		$info_data = $Obj->where('entrance_id = '.$entrance_id)->find();
    	}
		$this->assign("id_type", $id_type);
    	$this->assign("id_val", $id_val);
    	$this->assign('info_data',$info_data);
    	$this->display('add_entrance');
    }
    
    /**
     * 选择栏目弹窗
     */
    public function chooseEntrance(){
        $column_id = I("column_id") ? I("column_id") : '';
    	$area_id = I("area_id") ? I("area_id") : '';
    	$type_data = I("type_data") ? I("type_data") : '';   //id_名称
    	$id_type = '';
    	$id_val  = '';
    	if($column_id){
    		$id_type = "column_id";
    		$id_val  = $column_id;
    	}else{
    		$id_type = "area_id";
    		$id_val  = $area_id;
    	}
    	$entrance_id = I("entrance_id");
    	
    	if(!$id_val){
    		$this->error("参数错误！");
    	}
    	
    	//获取所有可选入口
    	$where = array();
    	if($id_val) $where[$id_type] = $id_val;
    	$where['status'] = '0';
    	$where['entrance_type_id'] = array('EXP','IS NULL');
    	$choose_column_entrance_data = $this->news_column_entrance_model->GetColumnEntranceList($where,'0','9999');
    	$choose_column_entrance_list = $choose_column_entrance_data['column_entrance_list'];

		$this->assign("id_type", $id_type);
    	$this->assign("id_val", $id_val);
    	$this->assign("type_data", $type_data);
    	$this->assign('choose_data',$choose_column_entrance_list);
    	$this->display('choose_entrance');
    }
    
    /**
     * 添加选择的入口
     */
    public function addChooseEntrance(){
    	$column_id = I("column_id") ? I("column_id") : '';
    	$area_id = I("area_id") ? I("area_id") : '';
    	$id_type = '';
    	$id_val  = '';
    	if($column_id){
    		$id_type = "column_id";
    		$id_val  = $column_id;
    	}else{
    		$id_type = "area_id";
    		$id_val  = $area_id;
    	}
    	
    	$entrance_id = I("entrance_id") ? I("entrance_id") : '';
    	$type_data = I("type_data") ? I("type_data") : '';
    	$weight = I("weight") ? I("weight") : '';
    	if(!$id_val || !$type_data || !$entrance_id){
    		echo "参数错误！";exit;
    	}
    	if($type_data){
    		$type_arr = explode('_',$type_data);
    		$type_id = $type_arr[0];
    		$type_title = $type_arr[1];
    	}
    	 
    	//获取所有可选入口
    	$data = array();
    	$data['entrance_type_id'] = $type_id;
    	$data['entrance_type_title'] = $type_title;
    	$data['weight'] = $weight;
    	$Obj = M("vrnews1.column_entrance", 'news_');
    	$result = $Obj->where(array($id_type=>$id_val,'entrance_id'=>$entrance_id))->data($data)->save();
    	if($result !== false){
    		echo '1';
    	}else{
    		echo '2';
    	}
    }
    
    
    /**
     * 添加入口
     */
    public function addColumnEntrance(){
    	$id_type = '';
    	$id_val  = '';
    	
    	if(I("column_id")){
    		$id_type = "column_id";
    		$id_val  = I("column_id");
    	}else{
    		$id_type = "area_id";
    		$id_val  = I("area_id");
    	}
    	
    	$entrance_id = I("entrance_id");
    	
    	if(empty(I("post.title"))){
    		return $this->error('标题不能为空!');exit;
    	}
    	
    	if(mb_strlen(trim(I("post.title")),'UTF8') > 5){
    		return $this->error("标题不能超过5个字");
    	}
    	
    	if(empty($_FILES['imageFile']['name']) && empty($entrance_id)){
    		return $this->error('请上传封面图!');exit;
    	}elseif($entrance_id && empty($_FILES['imageFile']['name'])){
    		$data['cover_image'] = I('image_url');
    	}else{
    		$newsModel = new NewsModel();  //上传封面图
    		$imageUrl = $newsModel->uploadImage();
    		$data['cover_image'] = $imageUrl;
    	}
    	
    	if(empty(I("post.link_url"))){
    		return $this->error('url不能为空!');exit;
    	}
    		
    	
    	$data['title']   = I("post.title");
    	$data['column_id']   = I("post.column_id");
    	$data['area_id']   = I("post.area_id");
    	$data['subtitle']   = I("post.subtitle");
    	$data['link_url']   = I("post.link_url");
    	$data['link_type']   = I("post.link_type");
    	
    	
    	$typeObj = M("vrnews1.column_entrance", 'news_');
    	if(!empty($entrance_id)){
    		$type = $typeObj->where('entrance_id = '.$entrance_id)->data($data)->save();
    	}else{
    		$data['create_time'] = date('Y-m-d H:i:s',time());
    		$data['status'] = '0';
    		$type = $typeObj->data($data)->add();
    	}
    	
    	if($type){
    		$this->success('发布成功！', U("Admin/NewsColumnEntrance/index",array($id_type=>$id_val)));
    	}else{
    		$this->success('发布失败！', U("Admin/NewsColumnEntrance/index",array($id_type=>$id_val)));
    	}
    }
    
    

    
    /**
     * 修改入口信息
     */
    public function editColumnEntrance(){
    	$entrance_id = I("entrance_id");
    	$value   = I("val");
    	$type    = I("type");
    	if($type == 'title'){
    		$data['title'] = $value;
    	}else if($type == 'weight'){
    		$data['weight'] = $value;
    	}else if($type == 'subtitle'){
    		$data['subtitle'] = $value;
    	}
    	$Obj = M("vrnews1.column_entrance", 'news_');
    	echo $Obj->where(array('entrance_id' => $entrance_id))->save($data);exit;
    }
    
    
    /**
     * 删除入口
     */
    public function del(){
    	$entrance_id = I("entrance_id");
    	if(!$entrance_id){
    		$this->error("参数错误！");
    	}
    	$Obj = M("vrnews1.column_entrance", 'news_');
    	$result  = $Obj->where(array('entrance_id'=>$entrance_id))->delete();
    	if ($result) {
    		$this->success("删除成功！");
    	} else {
    		return $this->error($result['Message']);
    	}
    }
    
    /**
     * 撤销入口
     */
    public function revoke(){
    	$entrance_id = I("entrance_id");
    	if(!$entrance_id){
    		$this->error("参数错误！");
    	}
    	$data['status'] = '0';
    	$data['entrance_type_id'] = null;
    	$data['entrance_type_title'] = null;
    	$data['weight'] = null;
    	
    	$Obj = M("vrnews1.column_entrance", 'news_');
    	$result  = $Obj->where(array('entrance_id'=>$entrance_id))->data($data)->save();
    	if ($result !==false) {
    		$this->success("删除成功！");
    	} else {
    		return $this->error($result['Message']);
    	}
    }
    
    
    /**
     * 清空栏目下所有已选择的入口
     */
    public function emptyEntrance(){
    	$id_type  = I("id_type");
    	$id_val  = I("id_val");
    	if(!$id_type || !$id_val){
    		echo false;exit;
    	}
    
    	$data = array();
    	$data['status'] = '0';
    	$data['entrance_type_id'] = null;
    	$data['entrance_type_title'] = null;
    	$data['weight'] = null;
		$result = $this->news_column_entrance_model->where(array($id_type=>$id_val))->data($data)->save();
		if ($result !==false) {
			echo '1';exit;
		}
    }
    
    
    /**
     * 发布
     */
    public function releaseEntrance(){
        $id_type = I("id_type");
    	$id_val  = I("id_val");
    	
    	if(!$id_type || !$id_val){
    		echo false;exit;
    	}
    
    	$data = array();
    	$data['status'] = '1';
    	$where = array();
    	$where[$id_type] = $id_val;
    	$where['entrance_type_id'] = array('EXP','IS NOT NULL');
    	$result = $this->news_column_entrance_model->where($where)->data($data)->save();
    	if ($result !==false) {
    		echo '1';exit;
    	}
    }

    
    
    
    
    /**
     * 入口管理列表
     */
    function entranceManageList()
    {
    	$column_id = I("column_id") ? I("column_id") : '';
    	$area_id = I("area_id") ? I("area_id") : '';
    	$id_type = '';
    	$id_val  = '';
    	if($column_id){
    		$id_type = "column_id";
    		$id_val  = $column_id;
    	}else{
    		$id_type = "area_id";
    		$id_val  = $area_id;
    	}
    	if(!$id_val){
    		$this->error("未选中栏目！");
    	}
    	$currentPage = I('param.p', 0);
    	if ($currentPage <= 0) {
    		$currentPage = 1;
    	}
    	 
    	$pageSize = '20';
    	$pageStart = ($currentPage - 1) * $pageSize;
    	$pageEnd = $currentPage * $pageSize;
    	 
    	$where = array();
    	if($id_val) $where[$id_type] = $id_val;
    	$where['entrance_type_id'] = array('EXP','IS NOT NULL');
    	$column_entrance_data = $this->news_column_entrance_model->GetColumnEntranceList($where,$pageStart,$pageSize,'weight');
    	$column_entrance_list = $column_entrance_data['column_entrance_list'];
    	$count = $column_entrance_data['count'];
    	//获取当前栏目下的类型(获取列表第一条对应的状态)
    	$first_type_id = '';
    	$first_type_title = '';
    	//类型列表
    	$type_list = $this->news_column_entrance_type_model->GetTypeList();

    	if($column_entrance_list){
    		$first_type_id = $column_entrance_list[0]['entrance_type_id'];
    		$first_type_title = $column_entrance_list[0]['entrance_type_title'];
    		//获取有数据的情况下此类型的数量
    		$type_num = $this->GetNumByTypeId($first_type_id);
    		$type_symbol  = $this->news_column_entrance_type_model->where('type_id = '.$first_type_id)->getField('type');
    	}
    	
    	

    	$page = $this->page($count ,10);
    	$this->assign("page", $page->show('Admin'));
    	$this->assign("list", $column_entrance_list);
    	$this->assign("count", $count);
    	$this->assign("type_list", $type_list);
    	$this->assign("first_type_id", $first_type_id);
    	$this->assign("first_type_title", $first_type_title);
    	$this->assign("type_num", $type_num);
    	$this->assign("type_symbol", $type_symbol);
    	$this->assign("id_type", $id_type);
    	$this->assign("id_val", $id_val);
    	$this->display('managelist');
    }
    
    
    function GetNumByTypeId($type_id){
    	$num = '0';
    	if($type_id){
    		$num = $this->news_column_entrance_type_model->where('type_id = '.$type_id)->getField('num');
    	}
    	return $num;
    }
    
    
    /**
     * 入口类型列表
     */
    function type_index(){
    	$column_id = I("column_id") ? I("column_id") : '';
    	$area_id = I("area_id") ? I("area_id") : '';
    	$id_type = '';
    	$id_val  = '';
    	if($column_id){
    		$id_type = "column_id";
    		$id_val  = $column_id;
    	}else{
    		$id_type = "area_id";
    		$id_val  = $area_id;
    	}
    	if(!$id_val){
    		$this->error("未选中栏目！");
    	}
    	$currentPage = I('param.p', 0);
    	if ($currentPage <= 0) {
    		$currentPage = 1;
    	}
    	 
    	$pageSize = '20';
    	$pageStart = ($currentPage - 1) * $pageSize;
    	$pageEnd = $currentPage * $pageSize;
    	 
    	$type_data = $this->news_column_entrance_type_model->GetTypeIndex($pageStart,$pageSize);
    	$type_list = $type_data['list'];
    	$count = $type_data['count'];
    	
    	$page = $this->page($count ,10);
    	$this->assign("page", $page->show('Admin'));
    	$this->assign("list", $type_list);
    	$this->assign("id_type", $id_type);
    	$this->assign("id_val", $id_val);
    	$this->display();
    }
    
    
    /**
     * 添加入口类型页面
     */
    public function addEntranceType(){
    	$column_id = I("column_id") ? I("column_id") : '';
    	$area_id = I("area_id") ? I("area_id") : '';
    	$id_type = '';
    	$id_val  = '';
    	if($column_id){
    		$id_type = "column_id";
    		$id_val  = $column_id;
    	}else{
    		$id_type = "area_id";
    		$id_val  = $area_id;
    	}
    	$type_id = I("type_id");
    
    	if(!$id_val){
    		$this->error("参数错误！");
    	}
    	$info_data = '';
    	if($type_id){
    		$info_data = $this->news_column_entrance_type_model->where('type_id = '.$type_id)->find();
    	}
    	$this->assign("id_type", $id_type);
    	$this->assign("id_val", $id_val);
    	$this->assign('info_data',$info_data);
    	$this->display('add_type');
    }
    
    
    /**
     * 添加入口类型动作
     */
    public function addColumnEntranceType(){
    	$id_type = '';
    	$id_val  = '';
    	 
    	if(I("column_id")){
    		$id_type = "column_id";
    		$id_val  = I("column_id");
    	}else{
    		$id_type = "area_id";
    		$id_val  = I("area_id");
    	}
    	 
    	$type_id = I("type_id");
    	 
    	if(empty(I("post.title"))){
    		return $this->error('标题不能为空!');exit;
    	}
    	
    	if(empty(I("post.num"))){
    		return $this->error('数量不能为空!');exit;
    	}

    	 
    	$data['title']   = I("post.title");
    	$data['num']   = I("post.num");
    	$data['type']   = I("post.type");
    	 
    	 
    	$typeObj = M("vrnews1.column_entrance_type", 'news_');
    	if(!empty($type_id)){
    		$type = $typeObj->where('type_id = '.$type_id)->data($data)->save();
    	}else{
    		$type = $typeObj->data($data)->add();
    	}
    	 
    	if($type){
    		$this->success('发布成功！', U("Admin/NewsColumnEntrance/type_index",array($id_type=>$id_val)));
    	}else{
    		$this->success('发布失败！', U("Admin/NewsColumnEntrance/type_index",array($id_type=>$id_val)));
    	}
    }
    
    
    /**
     * 修改入口类型信息
     */
    public function editColumnEntranceType(){
    	$type_id = I("type_id");
    	$value   = I("val");
    	$type    = I("type");
    	if($type == 'title'){
    		$data['title'] = $value;
    	}else if($type == 'weight'){
    		$data['weight'] = $value;
    	}else if($type == 'num'){
    		$data['num'] = $value;
    	}
    	$Obj = M("vrnews1.column_entrance_type", 'news_');
    	if($Obj->where(array('type_id' => $type_id))->save($data) !== false){
    		if($type == 'title'){
    			$data1['entrance_type_title'] = $value;
    			if($this->news_column_entrance_model->where('entrance_type_id = '.$type_id)->data($data1)->save() === false){
    				echo '0';exit;
    			}
    		}
    		echo '1';exit;
    	}
    	//echo $Obj->where(array('type_id' => $type_id))->save($data);exit;
    }
    
    
    /**
     * 删除入口类型
     */
    public function del_type(){
    	$type_id = I("type_id");
    	if(!$type_id){
    		$this->error("参数错误！");
    	}
    	$Obj = M("vrnews1.column_entrance_type", 'news_');
    	$result  = $Obj->where(array('type_id'=>$type_id))->delete();
    	if ($result !== false) {
    		//删除所有入口管理中已选的入口
    		$data['entrance_type_id'] = NULL;
    		$data['entrance_type_title'] = NULL;
    		$this->news_column_entrance_model->where('entrance_type_id = '.$type_id)->data($data)->save();
    		$this->success("删除成功！");
    	} else {
    		return $this->error($result['Message']);
    	}
    }
    


}